package com.think.oauth.user.qh.cas;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.provider.TokenRequest;

public abstract class CasUtils2 {

	/** 判断是否已经登录过 */
	public static boolean isLogin(HttpSession session) {
		Object isLogin = session.getAttribute(Constants.LOGIN_KEY);
		return BooleanUtils.toBoolean(String.valueOf(isLogin));
	}

	public static String getBasePath() {
		return "https://ilab.arvroffer.com/thinkfront/home";

	}


	/** 判断票据是否存在 */
	public static boolean hasTicket(TokenRequest request) {
		Object ticket = request.getRequestParameters().get(Constants.TICKET_KEY);
		// System.out.println("ticket = " + ticket);
		return ticket != null
				&& !StringUtils.isEmpty(String.valueOf(ticket));
	}

	public static String getURLEncodeServiceUrl(TokenRequest request)
			throws UnsupportedEncodingException {

		// 编码成系统可识别的加密串

		String serviceUrlRoot = getBasePath();
		
		return URLEncoder.encode(serviceUrlRoot, Constants.UTF_8_STR);
	}

	/** 获取Cas登录Url 登录成功后返回票据 */
	public static String getLoginUrl(TokenRequest request)
			throws UnsupportedEncodingException {
		String encodeServiceUrl = getURLEncodeServiceUrl(request);

		return Constants.CAS_LOGIN_URL + "?" + Constants.SERVICE_KEY + "="
				+ encodeServiceUrl;
	}

	/** 获取校验票据Url */
	public static String getServiceValidateUrl(TokenRequest request)
			throws UnsupportedEncodingException {

		String encodeServiceUrl = getURLEncodeServiceUrl(request);
		System.out.println("encodeServiceUrl = " + encodeServiceUrl);
		Object ticket = request.getRequestParameters().get(Constants.TICKET_KEY);

		// System.out.println("ticket = " + ticket);

		return Constants.CAS_VALIDATE_URL + "?" + Constants.TICKET_KEY
				+ "=" + ticket + "&" + Constants.SERVICE_KEY + "="
				+ encodeServiceUrl;
	}

	public static LoginUser getLoginUser(TokenRequest request)
			throws IOException {	
		String casUserInfoXml = "";
		try {
			String serviceValidateUrl = getServiceValidateUrl(request);
	
			System.out.println("serviceValidateUrl = " + serviceValidateUrl);
	
			casUserInfoXml = HttpRequestUtils.doGet(serviceValidateUrl);
			
			casUserInfoXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + casUserInfoXml;
	
			//System.out.println("casUserInfoXml = " + casUserInfoXml);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new LoginUser(casUserInfoXml);
	}

	/** 获取登出地址 */
	public static String getLogoutUrl(TokenRequest request)
			throws UnsupportedEncodingException {
		// 获取本次请求的根Path
		String loginUrlRoot = getBasePath()
				+ Constants.SSO_LOGIN_URI;
		String encodeLoginUrlRoot = URLEncoder.encode(loginUrlRoot,
				Constants.UTF_8_STR);
		
		String logoutUrl = Constants.CAS_LOGOUT_URL + "?" + Constants.SERVICE_KEY + "="
		+ encodeLoginUrlRoot;
		
		// 获取请求中的targetUrl
		String targetUrl = request.getRequestParameters().get(Constants.TARGET_URL_KEY);
		
        if (!StringUtils.isEmpty(targetUrl)) {
        	logoutUrl += "?" + Constants.TARGET_URL_KEY + "=" + targetUrl;
		}
        
        System.out.println("logoutUrl === " + logoutUrl); 
		
		return logoutUrl;
	}
	
	/** 集成失败页 */
	public static String getErrorUrl(HttpServletRequest request)
			throws UnsupportedEncodingException {

		return Constants.SSO_ERROR_URI;
	}

	/** 写入单页面登录判断标志 */
	public static void login(LoginUser loginUser, HttpSession session) {
		session.setAttribute(Constants.LOGIN_KEY, true);
		session.setAttribute(Constants.LOGIN_USER_KEY, loginUser);
	}

	/** 移出单页面登录判断标志 */
	public static void logout(HttpSession session) {
		session.removeAttribute(Constants.LOGIN_KEY);
		session.removeAttribute(Constants.LOGIN_USER_KEY);
	}

}
